package com.hgkj.cloud.base.util;

import com.hgkj.cloud.base.common.ResponseData;
import org.slf4j.Logger;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class ResponseUtil {
    public static ResponseData getResponseData(Map<String, Object> resultMap, Integer code, String msg) {
        ResponseData retData = new ResponseData();
        retData.setData(resultMap);
        if (code != null) {
            retData.setCode(code);
        }
        if (msg != null) {
            retData.setMsg(msg);
        }
        return retData;
    }

    public static ResponseData getResponseData(Integer code, String msg) {
        return getResponseData(null, code, msg);
    }

    public static ResponseData getResponseData(Integer code, String msg, Logger log) {
        log.info("@@@@@@ Response -> code:{}, msg:{}", code, msg);
        return getResponseData(null, code, msg);
    }

    public static ResponseData getResponseDataAndLog(Map<String, Object> resultMap, Integer code, String msg, Logger log) {
        log.info("@@@@@@ Response -> code:{}, msg:{}, resultMap:{}", code, msg, resultMap);
        return getResponseData(resultMap, code, msg);
    }

    public static ResponseData getResponseDataAndLog(ResponseData responseData, Logger log) {
        log.info("@@@@@@ Response -> code:{}, msg:{}, resultMap:{}", responseData.getCode(), responseData.getMsg(), responseData.getData());
        return responseData;
    }

    public static ResponseData getExtResponseDataAndLog(Map<String, Object> resultMap, Logger log) {
        log.info("@@@@@@ Response -> resultMap:{}", resultMap);
        return getResponseData(resultMap, null, null);
    }

    public static ResponseData getResponseData(Map<String, Object> resultMap) {
        return getResponseData(resultMap, null, null);
    }

    public static ResponseData getResponseData() {
        return getResponseData(null);
    }

    public static void set(HttpServletResponse response,
                           String name,
                           String value,
                           int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }
}
